package finger.dao.impl;

import finger.dao.BookDao;
import finger.domain.Book;

import java.util.List;

public class BookDaoImpl extends BaseDao implements BookDao {


    @Override
    public int addBook(Book book) {
        String sql = "INSERT INTO t_book(`name` , `author` , `price` , `sales` , `stock` , `img_path`) " +
                "values(?,?,?,?,?,?)";
        return update(sql, book.getName(), book.getAuthor(), book.getPrice(), book.getSales(), book.getStock(), book.getImgPath());
    }

    @Override
    public int deleteBook(Integer id) {
        String sql = "delete from t_book where id = ?";
        return update(sql, id);
    }


    public int updateBook(Book book) {
        String sql = "update t_book set`name`=? , `author`=? , `price`=? , `sales`=? , `stock`=? , `img_path`=? where id = ?";
        return update(sql, book.getName(), book.getAuthor(), book.getPrice(), book.getSales(), book.getStock(), book.getImgPath(), book.getId());
    }

    @Override
    public Book queryBookById(Integer id) {
        String sql = "select `id`,`name`,`author`,`price`,`sales`,`stock`,img_path imgPath from t_book where id = ?";
        return getQueryForOne(Book.class, sql, id);
    }

    @Override
    public List<Book> queryBooks() {
        String sql = "select `id`,`name`,`author`,`price`,`sales`,`stock`,img_path imgPath from t_book";
        return getQueryForList(Book.class, sql);
    }

    @Override
    public Integer queryPageTotalCount() {
        String sql = "select count(*) from t_book";
        Number number = (Number) queryForSingleValue(sql);
        return number.intValue();
    }

    @Override
    public List<Book> queryForItems(Integer i, Integer pageSize) {
        String sql = "select `id`,`name`,`author`,`price`,`sales`,`stock`,img_path imgPath from t_book limit ?,?";
        return getQueryForList(Book.class,sql,i,pageSize);
    }

    @Override
    public Integer queryPageTotalCountByPrice(Integer min, Integer max) {
        String sql = "select count(*) from t_book where price between ? and ?";
        Number number = (Number) queryForSingleValue(sql, min, max);
        return number.intValue();
    }

    @Override
    public List<Book> queryForItemsByPrice(Integer begin,Integer pageSize,Integer min, Integer max) {
        String sql = "select `id`,`name`,`author`,`price`,`sales`,`stock`,img_path imgPath from t_book where price between ? and ? order by price limit ?, ?";
        return getQueryForList(Book.class,sql,min,max,begin,pageSize);
    }
}
